//
//  MyTabBarController.m
//  wifi4.2
//
//  Created by 郭四虎 on 15/11/13.
//  Copyright © 2015年 YouKu. All rights reserved.
//
#import <CoreLocation/CoreLocation.h>
#import "MyTabBarController.h"
#import "HomeViewController.h"
#import "GamecommentViewController.h"

#import "AVViewController.h"
#import "MeViewController.h"

#include "NewsViewController.h"

@interface MyTabBarController ()<CLLocationManagerDelegate,UITabBarControllerDelegate>
{
    CLLocationManager *_manager;
    NSArray *_controllers;
}
@end

@implementation MyTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
   // _dataDic = [NSDictionary dictionary];
    [self getLocation];
    
//    [self createContent];
}
- (instancetype)initWithDic:(NSDictionary *)dic
{
    if (self = [super init]) {
        self.dataDic = dic;
        [self createContent];
    }
    return self;
}
- (instancetype)initWithBool:(BOOL)notShowAd
{
    if (self = [super init]) {
        self.notShowAd = notShowAd;
        [self createContent];
    }
    return self;
}
- (void)createContent
{
    NSArray *titles = @[@"新闻",@"游戏",@"服务",@"视频",@"我"];
    NSArray *imageNames = @[@"新闻",@"游戏",@"发现",@"视听",@"我"];
    NSArray *selectedNames = @[@"新闻-拷贝",@"游戏-拷贝",@"发现-拷贝",@"视听-拷贝",@"我-拷贝",];
    
    UINavigationController *news = [self createNavigation:[[NewsViewController alloc]init] withTitle:titles[0] andImagename:imageNames[0] andSelectname:selectedNames[0]];
    UINavigationController *game = [self createNavigation:[[GamecommentViewController alloc]init] withTitle:titles[1] andImagename:imageNames[1] andSelectname:selectedNames[1]];
    UINavigationController *home = [self createNavigation:[[HomeViewController alloc]init] withTitle:titles[2] andImagename:imageNames[2] andSelectname:selectedNames[2]];
    UINavigationController *av = [self createNavigation:[[AVViewController alloc]init]withTitle:titles[3] andImagename:imageNames[3] andSelectname:selectedNames[3]];
    UINavigationController *me = [self createNavigation:[[MeViewController alloc]init] withTitle:titles[4] andImagename:imageNames[4] andSelectname:selectedNames[4]];
    
    _controllers = @[home,game,news,av,me];
    
    self.viewControllers = _controllers;
    self.tabBar.tintColor = [UIColor orangeColor];
    self.selectedIndex = 2;
    self.delegate = self;
}

//- (void)checkNewVersion
//{
//    NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
//    NSString *currentVersion = [NSString stringWithFormat:@"%@",infoDic[@"CFBundleShortVersionString"]];
//    
//    NSLog(@"------------%@",currentVersion);
//    
//    NSDictionary *parameter = @{@"app_id":@"a0cbe699adc743b799f394c042957edc",@"version":currentVersion};
//    [MyAFN POST:APPVER parameters:parameter block:^(id responseObject) {
//        
//        NSDictionary *dic = responseObject;
//        if ([[dic objectForKey:@"status"]integerValue]) {
//            
//            UINavigationController *na = _controllers[4];
//            if ([dic objectForKey:@"res_path"]) {
//               [[NSUserDefaults standardUserDefaults]setObject:[dic objectForKey:@"res_path"] forKey:@"APPVER"];
//                [Myuser showRedPoint:na index:4];
//            }else{
//                [[NSUserDefaults standardUserDefaults]setObject:nil forKey:@"APPVER"];
//            }
//        }
//    } error:^(NSError *error) {
//        
//    } fromController:nil];
//}



- (void)getLocation
{
    _manager = [[CLLocationManager alloc]init];
    if (System >=8.0) {
        
        [_manager requestWhenInUseAuthorization];
        // 前台定位
    }
    
    _manager.delegate = self;
    
    if ([CLLocationManager locationServicesEnabled]) {
        
        [_manager startUpdatingLocation];
    }
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
    CLLocation *cl = [locations lastObject];
    //    getLongitude = cl.coordinate.longitude;
    //    getLatitude = cl.coordinate.latitude;
    [manager stopUpdatingLocation];
    
    NSString *lo = [NSString stringWithFormat:@"%.2f",cl.coordinate.longitude];
    NSString *la = [NSString stringWithFormat:@"%.2f",cl.coordinate.latitude];
    
    [[NSUserDefaults standardUserDefaults]setObject:lo forKey:@"longitude"];
    [[NSUserDefaults standardUserDefaults]setObject:la forKey:@"latitude"];
    
    CLGeocoder *geo = [[CLGeocoder alloc]init];
    [geo reverseGeocodeLocation:cl completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        
        if (placemarks.count >0) {
            
            CLPlacemark *place = placemarks[0];
            //地名
            NSLog(@"%@",place.name);
            //街道号
            NSLog(@"%@",place.thoroughfare);
            NSLog(@"%@",place.subThoroughfare);
            //市
            NSLog(@"%@",place.locality);
            //区
            NSLog(@"%@",place.subLocality);
            //省
            NSLog(@"%@",place.administrativeArea);
            NSLog(@"%@",place.postalCode);
            NSLog(@"%@",place.ISOcountryCode);
            //国家
            NSLog(@"%@",place.country);
            NSLog(@"%@",place.inlandWater);
            NSLog(@"%@",place.ocean);
            NSLog(@"%@",place.areasOfInterest);
        }
    }];
}



- (UINavigationController *)createNavigation:(UIViewController *)controller withTitle:(NSString *)title andImagename:(NSString *)imageName andSelectname:(NSString *)selected
{
    
    if ([controller isKindOfClass:[HomeViewController class]]) {
        HomeViewController *vc = (HomeViewController *)controller;
        vc.dataDic = self.dataDic;
        vc.isError = self.isError;
    }
    if (self.notShowAd) {
        if ([controller isKindOfClass:[NewsViewController class]]) {
            NewsViewController *vc = (NewsViewController *)controller;
            vc.notShowAd = self.notShowAd;
        }
    }
    
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:controller];
    
    UITabBarItem *item = [[UITabBarItem alloc]initWithTitle:title image:[UIImage imageNamed:imageName] selectedImage:[UIImage imageNamed:selected]];
    
    nav.tabBarItem = item;
    
    return nav;
}

//- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
//{
//    NSLog(@"%lu",(unsigned long)tabBarController.selectedIndex);
//}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
